package com.xiaoyg.reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;

public class Student {

    private String studentName;

    public int studentAge;

    public Student(){

    }

    public Student(String studentName){
        this.studentName = studentName;
    }

    public void setStudentAge(int studentAge){
        this.studentAge = studentAge;
    }

    private String show(String message){
        System.out.printf("show: "+studentName+","+studentAge);
        return "testReturnValue";
    }

    public static void main(String[] args) throws ClassNotFoundException {
        //获取class对象
        Class studentClass1 = Class.forName("com.xiaoyg.reflect.Student");

        Class studentClass2 = Student.class;

        Student student = new Student();

        Class studentClass3 = student.getClass();

        System.out.println("class1 = " +studentClass1+"\n"
        +"class2 = "+studentClass2+"\n"
                +"class3 = "+studentClass3+ "\n"
                +"class1 == class2 ?"+(studentClass1 == studentClass2) +"\n"
                +"class2 == class3 ?"+(studentClass2 == studentClass2)
        );

        //获取成员变量

        System.out.println("获取属性开始： =====================================================");

        Field[] declaredFields = studentClass1.getDeclaredFields();

        for (Field declaredField : declaredFields) {
            System.out.println("declared Field: "+declaredField);
        }

        Field[] fields = studentClass1.getFields();
        for (Field field : fields) {
            System.out.println("field: "+ field);
        }

        System.out.println("获取属性结束： =====================================================");

        System.out.println("获取构造方法开始： =====================================================");

        Constructor[] declaredConstructorList = studentClass1.getDeclaredConstructors();

        for (Constructor declaredConstructor : declaredConstructorList) {
            System.out.println("declared constructor: "+ declaredConstructor);
        }

        Constructor[] constructorList = studentClass1.getConstructors();

        for (Constructor constructor : constructorList) {
            System.out.println("constructor: "+constructor);
        }

        System.out.println("获取构造方法结束： =====================================================");

        System.out.println("获取非构造方法开始： =====================================================");

        //非构造函数
        Method[] declaredMethodList = studentClass1.getDeclaredMethods();
        for (Method  declaredMethod : declaredMethodList) {
            System.out.println("declared Method: "+ declaredMethod);
        }

        Method[] methodList = studentClass1.getMethods();
        for (Method method : methodList) {
            System.out.println("method: "+ method);
        }
        System.out.println("获取非构造方法结束： =====================================================");



    }

}
